<template>
  <div class="restock-container">
    <el-card>
      <el-form :inline="true"
               ref="search_data"
               class="demo-form-inline">
        <el-row>
          <el-form-item label="商品">
            <el-input v-model="search_data.title"
                      placeholder="输入商品名称" />
          </el-form-item>
          <el-form-item label="价格范围">
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-input v-model="search_data.min_price"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line"
                    :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="date2">
                <el-input v-model="search_data.max_price"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"
                       @click="onSearch">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="never"
             style="margin-top:10px;">
      <div class="main-card">
        <div>
          <span style="font-size:18px;font-weight:900;">进货单</span>
        </div>
        <div>
          <el-button icon="el-icon-plus"
                     type="primary"
                     @click="handleAdd()">新增</el-button>
        </div>
      </div>
      <el-table :data="table_data"
                tooltip-effect="dark"
                style="width: 100%">
        <el-table-column prop="title"
                         label="名称">
        </el-table-column>
        <el-table-column prop="supplier"
                         label="供应商">
        </el-table-column>
        <el-table-column prop="price"
                         label="总价">
        </el-table-column>
        <el-table-column prop="weight"
                         label="进货">
        </el-table-column>
        <el-table-column label="单价">
          <template slot-scope="scope">
            <div>{{scope.row.weight==0?0:(scope.row.price/scope.row.weight).toFixed(2)}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="sell"
                         label="售出">
        </el-table-column>
        <el-table-column prop="buy_time"
                         label="采购日期">
        </el-table-column>
        <el-table-column label="操作"
                         width="100">
          <template slot-scope="scope">
            <el-tooltip class="item"
                        effect="dark"
                        content="出货"
                        placement="top-start">
              <i class="el-icon-position"
                 @click="handleShipment(scope.row.id)"
                 style="margin-left:5px"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="出货"
               :visible.sync="dialogShipmentVisible"
               width="500px">
      <el-form :model="form"
               label-width="80px">

        <el-form-item label="出货方式">
          <el-select v-model="form_shipment.type"
                     placeholder="请选择"
                     style="width:100%">
            <el-option label="自饮"
                       :value="1">
            </el-option>
            <el-option label="送礼"
                       :value="2">
            </el-option>
            <el-option label="售出"
                       :value="3">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出货客户">
          <el-select v-model="form_shipment.customer_id"
                     filterable
                     placeholder="请选择">
            <el-option v-for="item in customer_data"
                       :key="item.id"
                       :label="item.title"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出货数量">
          <el-input v-model="form_shipment.weight"></el-input>
        </el-form-item>

        <el-form-item label="出货价格">
          <el-input v-model="form_shipment.price"></el-input>
        </el-form-item>
        <el-form-item label="出货日期">
          <el-date-picker v-model="form_shipment.sell_time"
                          value-format="yyyy-MM-dd"
                          type="date"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogShipmentVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="handleSellData">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增/编辑"
               :visible.sync="dialogFormVisible"
               width="500px">
      <el-form :model="form"
               label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="供应商">
          <el-input v-model="form.supplier"></el-input>
        </el-form-item>
        <el-form-item label="总价">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="斤数">
          <el-input v-model="form.weight"></el-input>
        </el-form-item>
        <el-form-item label="采购日期">
          <el-date-picker v-model="form.buy_time"
                          value-format="yyyy-MM-dd"
                          type="date"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="handleAddData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getList, addData, sellData } from '@/api/restock'
import { getList as getCustomerList } from '@/api/customer'
export default {
  data () {
    return {
      search_data: {
        page: 1,
        limit: 10,
        title: '',
        min_price: '',
        max_price: '',
      },
      form: {
        title: '',
        supplier: '',
        price: '',
        weight: '',
      },
      form_shipment: {
        restock_id: '',
        weight: '',
        price: '',
        type: '',
        sell_time: '',
        customer_id: ''
      },
      all_count: 0,
      table_data: [],
      customer_data: [],
      dialogFormVisible: false,
      dialogShipmentVisible: false,
    }
  },
  created () {
    this.fetchData()
    this.fetchCustomerData()
  },
  methods: {
    onSearch () { },
    handleAdd () {
      this.dialogFormVisible = true
    },
    handleShipment (id, index) {
      this.form_shipment.restock_id = id
      this.dialogShipmentVisible = true
    },

    handleSellData () {
      sellData(this.form_shipment).then(response => {
        if (response.code == 1000) {
          this.$message({
            message: '出货成功',
            type: 'success'
          });
          this.dialogShipmentVisible = false
        } else {
          this.$message.error(response.msg);
        }
      })
    },
    handleAddData () {
      if (this.form.id) {
        editData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据保存成功',
              type: 'success'
            });
            this.dialogFormVisible = false
          } else {
            this.$message.error(response.msg);
          }
        })
      } else {
        addData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据添加成功',
              type: 'success'
            });
            this.dialogFormVisible = false
            this.fetchData()
          } else {
            this.$message.error(response.msg);
          }
        })
      }
    },
    fetchData () {
      getList(this.search_data).then(response => {
        this.table_data = response.data.data
        this.all_count = response.data.count
      })
    },
    fetchCustomerData () {
      getCustomerList().then(response => {
        this.customer_data = response.data.data
      })
    }
  }
}
  </script>

  <style lang="scss"
         scoped>
.restock-container {
  max-width: 1300px;
  margin: 0 auto;
  padding: 30px;
  .main-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
